/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024.
 * Description: hkids common interface.
 * Author: weiyuchen
 * Create: 2023-09-08
 */

#ifndef HKIDS_H
#define HKIDS_H

#include <linux/audit.h>
#include <linux/hkids/hkids_tracepoint.h>

#ifdef CONFIG_RTOS_HKIDS
#include <linux/hkids/hkids_path.h>
#include <linux/task_struct_extend.h>
#ifdef CONFIG_HKIDS_CHK_HEAP_SPRAY
#include <linux/sched/user.h>
#endif
#endif

#ifdef CONFIG_HKIDS_ENABLE_SWITCH
extern bool is_hkids_enabled(void);
#else
static inline bool is_hkids_enabled(void)
{
	return true;
}
#endif

#ifdef CONFIG_HKIDS_CHK_ABNORMAL_EXIT
extern void hkids_signal_deliver_trace(struct ksignal *ksig,
				struct task_struct *tsk);
#else
static inline void hkids_signal_deliver_trace(struct ksignal *ksig,
				struct task_struct *tsk)
{
}
#endif

#ifdef CONFIG_HKIDS_CHK_MLTDOWN
extern void task_clear_segv_count(struct task_struct *tsk);
#else
static inline void task_clear_segv_count(struct task_struct *tsk)
{
}
#endif

#ifdef CONFIG_HKIDS_CHK_CFI_EXCEPTION
extern void enable_hkids_chk_cfi_exception(bool value);
extern bool is_hkids_chk_cfi_exception_enabled(void);

#else
static inline void enable_hkids_chk_cfi_exception(bool value)
{
}
static inline bool is_hkids_chk_cfi_exception_enabled(void)
{
	return false;
}
#endif

#ifdef CONFIG_HKIDS_CHK_KO_SIG
extern void enable_hkids_chk_ko_sig(bool value);
extern bool is_hkids_chk_ko_sig_enabled(void);
extern void hkids_report_abnormal_ko(long err);
#else
static inline void enable_hkids_chk_ko_sig(bool value)
{
}
static inline bool is_hkids_chk_ko_sig_enabled(void)
{
	return false;
}
static inline void hkids_report_abnormal_ko(long err)
{
}
#endif


#ifdef CONFIG_HKIDS_CHK_MAC_VIOLATION
enum chk_mac_control {
	/* don't report selinux log */
	CHK_MAC_VIOLATION_DISABLED = 0,
	/* report selinux log without speed limitation */
	CHK_MAC_VIOLATION_NO_SPEED_LIMIT = 1,
	/* report selinux log with per 10ms limitation */
	CHK_MAC_VIOLATION_MIN_SPEED_LIMIT = 10,
	/* report selinux log with per 100ms limitation */
	CHK_MAC_VIOLATION_MAX_SPEED_LIMIT = 100
};

extern int set_chk_mac_interval(unsigned int value);
extern unsigned int get_chk_mac_interval(void);
extern void hkids_chkmacatt_report(struct audit_buffer *ab,
	int result, u32 denied);
#else
static inline int set_chk_mac_interval(unsigned int value)
{
	return 0;
}
static inline unsigned int get_chk_mac_interval(void)
{
	return 0;
}
static inline void hkids_chkmacatt_report(struct audit_buffer *ab,
	int result, u32 denied)
{
}
#endif

#ifdef CONFIG_RTOS_HKIDS_RESPONSE
extern bool get_hkids_response_enabled(void);
#endif

#ifdef CONFIG_RTOS_HKIDS_AUDIT_REPORT
extern unsigned int hkids_get_log_seq(void);
extern int hkids_create_kbox_reg(size_t size);
extern void notrace hkids_report(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
#elif defined(CONFIG_EULEROS_HKIDS)
struct hkids_control_ops {
	void (*hkids_report)(const char *fmt, ...);
};
/* call sleep function, can't called by lock that don't support hibernation */
extern struct hkids_control_ops *hkids_control;
extern void resigter_hkids_control(struct hkids_control_ops *co);
extern void unresigter_hkids_control(struct hkids_control_ops *co);
#else
#define hkids_report(fmt, ...)  pr_warn(fmt "\n", ##__VA_ARGS__)
#endif

#endif
